let Api = require('../../utils/api.js');
let wxRequest = require('../../utils/wxRequest.js');
let common = require('../../utils/common.js')
let util = require("../../utils/util.js")

Page({
  data: {
    token: '',
    hospId: 0,
    hosp: '全部',
    ishospshow: false,
    hospcolumns: [],
    ks: '选择科室',
    ksId: '',
    isksshow: '',
    kscolumns: [],
    isdate1show: false,
    isdate2show: false,
    currentDate1: '',
    currentDate2: '',
    inputdate1: '',
    inputdate2: '',
    lzsr: 0,
    lwx: 0,
    lzfb: 0,
    ltyj: 0,
    lyj: 0,
    bedNum: 0,
    bedUse: 0,
    list: [],
  },
  onLoad: function (options) {
    let user = wx.getStorageSync('user')
    if (user) {
      let current = new Date().getTime()
      this.setData({
        currentDate1: current,
        currentDate2: current,
        token: user.code
      })
      common.initHosp(this)
      common.initDept(this)
    } else {
      wx.navigateTo({
        url: '../login/login',
      })
    }
  },
  formsubmit: function (e) {
    let self = this
    let value = e.detail.value
    let hosp = self.data.hospId
    let ks = self.data.ksId
    let lc = value.lc
    let date1 = value.date1
    let date2 = value.date2
    wx.showLoading({
      title: '正在统计...',
    })
    wxRequest.getRequest(Api.agTotal(self.data.token, date1, date2, hosp, ks, lc), null).then(res => {
      if (res.data) {
        self.show(res.data, lc)
      } else {
        wx.showModal({
          title: '提示',
          content: '统计失败!'
        })
      }
    }).finally(fina => {
      wx.hideLoading()
    })
  },
  show: function (adt, lc) {
    let lzsr = 0,
      lyj = 0,
      lwx = 0,
      lzfb = 0,
      ltyj = 0,
      bedNum = adt.mac.length
    bedUse = 0
    ol = adt.ord.length
    let list = new Array()
    for (let i = 0; i < bedNum; i++) {
      if (!util.isEmpty(lc)) {
        if (adt.mac[i].bdesc.indexOf(lc) < 0) {
          continue
        }
      }
      if (adt.mac[i].st == '1') {
        bedUse += 1
      }
      lzsr = 0
      lyj = 0
      for (let j = 0; j < ol; j++) {
        if (adt.mac[i].imac == adt.ord[j].imac) {
          if (adt.ord[j].tmo != '0') {
            lyj += adt.ord[j].yj
          }
          lzsr += adt.ord[i].csfy
        }
      }

      let item = {}
      item.sj = adt.mac[i].sj
      item.ks = adt.mac[i].ks
      item.bdesc = adt.mac[i].bdesc
      item.mac = adt.mac[i].mac
      item.lzsr = lzsr
      item.lyj = lyj
      item.st = adt.mac[i].st
      list.push(item)
    }

    lzsr = 0
    lyj = 0
    for (let i = 0; i < ol; i++) {
      lzsr += adt.ord[i].csfy
      if (adt.ord[i].pt == '1') {
        lwx += adt.ord[i].csfy
      } else {
        lzfb += adt.ord[i].csfy
      }
      if (adt.ord[i].tmo == '0') {
        ltyj += adt.ord[i].yj
      } else {
        lyj += adt.ord[i].yj
      }
    }

    self.setData({
      lzsr: lzsr,
      lwx: lwx,
      lzfb: lzfb,
      ltyj: ltyj,
      lyj: lyj,
      bedNum: bedNum,
      bedUse: bedUse,
      list: list,
    })
  },
  showhosptap: function () {
    this.setData({
      ishospshow: true
    })
  },
  showkstap: function () {
    this.setData({
      isksshow: true
    })
  },
  onhospcancel: function (e) {
    this.setData({
      ishospshow: false
    })
  },
  onkscancel: function () {
    this.setData({
      isksshow: false
    })
  },
  onhospconfirm: function (e) {
    var value = e.detail.value
    this.setData({
      hosp: value.text,
      hospId: value.id,
      ishospshow: false
    })
  },
  onksconfirm: function (e) {
    var value = e.detail.value
    this.setData({
      ks: value.text,
      ksId: value.id,
      isksshow: false
    })
  },
  showdate1tap: function () {
    this.setData({
      isdate1show: true
    })
  },
  showdate2tap: function () {
    this.setData({
      isdate2show: true
    })
  },
  confirmdate1: function (e) {
    var date = new Date(e.detail);
    this.setData({
      inputdate1: util.formatDate(date),
      isdate1show: false
    })
  },
  canceldate1: function () {
    this.setData({
      isdate1show: false
    })
  },
  canceldate2: function () {
    this.setData({
      isdate2show: false
    })
  },
  confirmdate2: function (e) {
    var date = new Date(e.detail);
    this.setData({
      inputdate2: util.formatDate(date),
      isdate2show: false
    })
  }
})